package com.jlf.starter.logger.reactive.provider;

import com.jlf.starter.logger.reactive.entity.LoggerConstant;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.util.StringUtils;


/**
 * 统一请求日志的请求body的记录器 post put
 *
 * @author wujr
 * 2023/8/3
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/8/3 1.0 新增]
 */
public final class BodyProvider {

//    public Mono<String> createBodyMessage(Mono<String> bodyMono) {
//        return bodyMono
//                .defaultIfEmpty(LoggerConstant.Message.noBody)
//                .map(this::createBodyMessage);
//    }

    public String createBodyMessage(FastByteArrayOutputStream bodyOutputStream) {
//        String body = new String(bodyOutputStream.toByteArray(), StandardCharsets.UTF_8);
        return createBodyMessage(bodyOutputStream.toString());
    }

//    public String createBodyMessage(DataBuffer bodyDataBuffer) {
//        return createBodyMessage(bodyDataBuffer.toString(Charset.defaultCharset()));
//    }

    public String createBodyMessage(String body) {
        return StringUtils.hasLength(body) ? body : LoggerConstant.Message.noBody;
    }
}
